/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.user;

import com.ivanbiz.departement.Departement;
import com.ivanbiz.departement.DepartementService;
import com.ivanbiz.injection.CallCtx;
import com.ivanbiz.properties.DepartementException;
import com.ivanbiz.properties.PasswordException;
import com.ivanbiz.properties.SISBeanConstants;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.properties.SISScreenConstants;
import com.ivanbiz.properties.UserNameException;
import com.ivanbiz.tools.IvanBiz;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author User
 */
public class UserAction extends IvanBiz {

    ActionMessages actionMessages = new ActionMessages();

    public ActionForward userLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
        DepartementService departementService = (DepartementService) CallCtx.getContext(request).getBean(SISBeanConstants.DEPARTEMENT_SERVICE);
        UserForm userForm = (UserForm) form;
        try {
            User user = userService.login(userForm.getUserName(), userForm.getPassword(), userForm.getDepartement());
            session.setAttribute(SISConstants.USER, user);
        } catch (UserNameException userNameException) {
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.USERNAME_EXCEPTION));
            saveMessages(request, actionMessages);
            return mapping.findForward(SISScreenConstants.WELCOME);
        } catch (PasswordException passwordException) {
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.PASSWORD_EXCEPTION));
            saveMessages(request, actionMessages);
            return mapping.findForward(SISScreenConstants.WELCOME);
        } catch (DepartementException departementException) {
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.DEPARTEMENT_EXCEPTION));
            saveMessages(request, actionMessages);
            return mapping.findForward(SISScreenConstants.WELCOME);
        } catch (Exception ex) {
            Logger.getLogger(UserAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return mapping.findForward(SISScreenConstants.WELCOME);
        }
        return mapping.findForward(SISScreenConstants.USER_LOGIN);
    }

    public ActionForward userLogout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        session.invalidate();
        return mapping.findForward(SISScreenConstants.WELCOME);
    }

    public ActionForward userList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
        DepartementService departementService = (DepartementService) CallCtx.getContext(request).getBean(SISBeanConstants.DEPARTEMENT_SERVICE);
        try {
            List listUser = userService.loadAll();
            request.setAttribute(SISConstants.LIST_USER, listUser);
            List listDepartement = departementService.loadAll();
            request.setAttribute(SISConstants.LIST_DEPARTEMENT, listDepartement);
        } catch (Exception ex) {
            Logger.getLogger(UserAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.USER_LIST);
    }

    public ActionForward userAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
        DepartementService departementService = (DepartementService) CallCtx.getContext(request).getBean(SISBeanConstants.DEPARTEMENT_SERVICE);
        UserForm userForm = (UserForm) form;
        try {
            boolean userIdIsUsed = userService.cekIsEmptyUserId(userForm.getUserName());
            if (userIdIsUsed) {
                actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.USER_NAME_IS_USED));
                saveMessages(request, actionMessages);
                return mapping.findForward(SISScreenConstants.USER_LIST);
            } else {
                User user = new User();
                user.setUserId(userForm.getUserName());
                user.setPassword(userForm.getPassword());
                user.setNama(userForm.getNama());
                Departement departement = departementService.loadById(userForm.getDepartement());
                user.setDepartement(departement);
                userService.saveUser(user);
                userForm.reset(mapping, request);
            }
        } catch (Exception ex) {
            Logger.getLogger(UserAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.USER);
    }

    public ActionForward userEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
        DepartementService departementService = (DepartementService) CallCtx.getContext(request).getBean(SISBeanConstants.DEPARTEMENT_SERVICE);
        UserForm userForm = (UserForm) form;
        try {
            User user = userService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            userForm.setUserName(user.getUserId());
            userForm.setPassword(user.getPassword());
            userForm.setNama(user.getNama());
            userForm.setDepartement(user.getDepartement().getId());
            List listDepartement = departementService.loadAll();
            request.setAttribute(SISConstants.LIST_DEPARTEMENT, listDepartement);
        } catch (Exception ex) {
            Logger.getLogger(UserAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.USER_EDIT);
    }

    public ActionForward userUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
        DepartementService departementService = (DepartementService) CallCtx.getContext(request).getBean(SISBeanConstants.DEPARTEMENT_SERVICE);
        UserForm userForm = (UserForm) form;
        try {
            boolean userIdIsUsed = userService.cekIsEmptyUserId(userForm.getUserName());
            if (userIdIsUsed) {
                actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.USER_NAME_IS_USED));
                saveMessages(request, actionMessages);
                return mapping.findForward(SISScreenConstants.USER_EDIT);
            } else {
                User user = userService.loadById(userForm.getId());
                user.setUserId(userForm.getUserName());
                user.setPassword(userForm.getPassword());
                user.setNama(userForm.getNama());
                Departement departement = departementService.loadById(userForm.getDepartement());
                user.setDepartement(departement);
                userService.updateUser(user);
                userForm.reset(mapping, request);
            }
        } catch (Exception ex) {
            Logger.getLogger(UserAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.USER);
    }

    public ActionForward userDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
        UserForm userForm = (UserForm) form;
        try {
            User user = userService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            userService.deleteUser(user);
        } catch (Exception ex) {
            Logger.getLogger(UserAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.USER);
    }

    public ActionForward userSearch(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        return mapping.findForward(SISScreenConstants.USER);
    }
}